package edu.bw.controller;

import edu.bw.common.CommonResult;
import edu.bw.dto.InsertRoleForm;
import edu.bw.dto.SelectRoleByConditionForm;
import edu.bw.dto.UpdateRoleForm;
import edu.bw.po.Role;
import edu.bw.service.RoleService;
import edu.bw.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/role")
@CrossOrigin
public class RoleController {

    @Autowired
    private RoleService roleService;

   /**
     * 按条件查询角色信息
     * @param selectCondition
     * @return
     */
   @PostMapping("/selectRoleByCondition")
   public CommonResult selectRoleByCondition(@RequestBody SelectRoleByConditionForm selectCondition) {
       PageUtils pageUtils = roleService.selectRoleByCondition(selectCondition);
       return CommonResult.ok().put("page", pageUtils);
   }

    /**
     * 添加角色
     * 对应 InsertRoleServlet
     */
    @PostMapping("/insert")
    public CommonResult insertRole(@RequestBody InsertRoleForm insertRoleForm) {
        Integer rows = roleService.insert(insertRoleForm);
        return CommonResult.ok().put("rows", rows);
    }
    /**
     * 根据ID查询角色
     * 对应 SelectByIdServlet
     */
    @PostMapping("/selectById")
    public CommonResult selectById(@RequestBody Role role) {
        List<Map<String, Object>> list = roleService.selectById(role.getId());
        return CommonResult.ok(list.get(0));
    }

    /**
     * 更新角色信息
     * 对应 UpdateRoleServlet
     */
    @PostMapping("/update")
    public CommonResult updateRole(@RequestBody UpdateRoleForm updateRoleForm) {
        Integer rows = roleService.update(updateRoleForm);
        return CommonResult.ok().put("rows", rows);
    }
    /**
     * 批量删除角色
     * 对应 DeleteRoleServlet
     */
    @PostMapping("/deleteRoleByIds")
    public CommonResult deleteRoleByIds(@RequestBody Map<String, List<Integer>> params) {
        List<Integer> ids = params.get("ids");
        Integer rows = roleService.deleteRoleByIds(ids);
        return CommonResult.ok().put("rows", rows);
    }
    /**
     * 查询所有角色
     * 对应 SelectAllRole
     */
    @GetMapping("/selectAllRole")
    public CommonResult selectAllRole() {
        List<Map<String, Object>> list = roleService.selectAllRole();
        return CommonResult.ok().put("list", list);
    }
}